package com.greate.community.controller.master;

import com.greate.community.dao.admin.RBACMapper;
import com.greate.community.entity.Permission;
import com.greate.community.entity.Role;
import com.greate.community.entity.admin_master.RoleNeedPermission;
import com.greate.community.service.master.RBACService;
import com.greate.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleAdminController {
    @Autowired
    RBACService rbacService;

    /**
     * 获得角色信息
     * @return
     */
    @RequestMapping("/getRoles")
    public List<Role> getRoles(){
        List<Role> roles = rbacService.selectAllRoles();
        return roles;
    }

    /**
     * 获得角色拥有的权限信息
     * @param roleId
     * @return
     */
    @RequestMapping("/getPermissionByRoleId")
    public List<Integer> getPermissionByRoleId(int roleId){
        return rbacService.selectPermissionByRoleId(roleId);
    }

    /**
     * @param roleNeedPermission
     * @return
     */
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public String addRole(@RequestBody RoleNeedPermission roleNeedPermission){
        Role role = roleNeedPermission.getRole();
        if (role==null ){
            return CommunityUtil.getJSONString(500,"请输入角色信息");
        }
        if (role.getRoleDesc() ==null ||role.getRoleDesc().equals("")
        ||role.getRoleName()==null ||role.getRoleName().equals("")){
            return CommunityUtil.getJSONString(500,"请输入角色信息");
        }
        if (roleNeedPermission.getPermissions()==null || roleNeedPermission.getPermissions().size()==0){
            return CommunityUtil.getJSONString(500,"请选择权限信息");
        }
        System.out.println(roleNeedPermission);
        System.out.println(roleNeedPermission.getRole());
        System.out.println(roleNeedPermission.getPermissions());
        return   rbacService.addRole(roleNeedPermission.getRole(), roleNeedPermission.getPermissions());
    }

    /**
     * 更新角色
     * @param roleNeedPermission
     * @return
     */
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public String updateRole(@RequestBody RoleNeedPermission roleNeedPermission){
        if (roleNeedPermission.getRole().getRoleName()==null||
                roleNeedPermission.getRole().getRoleDesc()==null
        || roleNeedPermission.getRole()==null ||roleNeedPermission.getPermissions()==null ||
                roleNeedPermission.getPermissions().size()==0){
            return CommunityUtil.getJSONString(500,"输入信息有误");
        }
        System.out.println(roleNeedPermission);
        System.out.println(roleNeedPermission.getRole());
        System.out.println(roleNeedPermission.getPermissions());
        return rbacService.updateRole(roleNeedPermission.getRole(), roleNeedPermission.getPermissions());
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping("/deleteRole")
    public String deleteRole(int roleId){
        return rbacService.deleteRole(roleId);
    }

    /**
     * 设置角色的默认权限
     * @return
     */
    @RequestMapping(value = "/setDefaultRolePermission",method = RequestMethod.POST)
    public String setDefaultRolePermission(@RequestBody List<Integer> permissionIds){
        return rbacService.setDefaultRolePermission(permissionIds);
    }

    /**
     * 获得角色的默认权限
     * @return
     */
    @RequestMapping("/getDefaultRolePermission")
    public List<Integer> getDefaultRolePermission(){
        return  rbacService.getDefaultRolePermission();
    }

}
